<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="120px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="所属项目" prop="projectId">
						<el-select v-model="form.projectId" placeholder="请选择所属项目" @change="handleProjectChange">
							<el-option :key="index" :label="item.name" :value="item.id" v-for="(item, index) in projectData" />
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="成本项" prop="costTypeId">
						<el-select v-model="form.costTypeId" placeholder="请选择成本项">
							<el-option :key="index" :label="item.name" :value="item.id" v-for="(item, index) in filteredCostTypeData" />
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="数量" prop="quantity">
						<el-input v-model="form.quantity" type="number" placeholder="请输入数量" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="成本合计" prop="monthSubtotal">
						<el-input v-model="form.monthSubtotal" type="number" placeholder="请输入成本合计" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="月数" prop="months">
						<el-input v-model="form.months" type="number" placeholder="请输入月数" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="每月控制额" prop="monthLimit">
						<el-input v-model="form.monthLimit" type="number" placeholder="请输入每月控制额" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="备注" prop="remark">
						<el-input type="textarea" rows="5" v-model="form.remark" placeholder="请输入备注" maxlength="200" show-word-limit />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="IntProjectCostBudgetDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/internal/cost/intProjectCostBudget';
import { projectList, costTypeList } from '/@/api/internal/cost/intProject';
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);

const projectData = ref<any[]>([]);
const costTypeData = ref<any[]>([]);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	projectId: '',
	costTypeId: '',
	quantity: 0,
	monthSubtotal: 0,
	months: 0,
	monthLimit: 0,
	remark: '',
});

// 定义校验规则
const dataRules = ref({
	projectId: [{ required: true, message: '项目id不能为空', trigger: 'blur' }],
	costTypeId: [{ required: true, message: '成本项id不能为空', trigger: 'blur' }],
	quantity: [
		{ required: true, message: '数量不能为空', trigger: 'blur' },
		{ validator: rule.number, trigger: 'blur' },
	],
	monthSubtotal: [
		{ required: true, message: '成本合计不能为空', trigger: 'blur' },
		{ validator: rule.number, trigger: 'blur' },
	],
	months: [
		{ required: true, message: '月数不能为空', trigger: 'blur' },
		{ validator: rule.number, trigger: 'blur' },
	],
	monthLimit: [
		{ required: true, message: '每月控制额不能为空', trigger: 'blur' },
		{ validator: rule.number, trigger: 'blur' },
	],
});

const filteredCostTypeData = computed(() => {
	return costTypeData.value.filter((item) => item.selected == 1);
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取intProjectCostBudget信息
	if (id) {
		form.id = id;
		getintProjectCostBudgetData(id);
	}

	getProjectData();
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 获取项目列表数据
const getProjectData = () => {
	projectList().then((res) => {
		projectData.value = res.data;
	});
};

// 初始化表单数据
const getintProjectCostBudgetData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 所属项目联动
const handleProjectChange = async () => {
	costTypeData.value = [];
	form.costTypeId = '';
	costTypeList({ id: form.projectId }).then((res) => {
		costTypeData.value = res.data;
	});
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>
